﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using Beetle.Sl.EditorControl;

namespace SlTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            //foreach (var item in System.Windows.Media.Fonts.SystemTypefaces)
            //{
            //    GlyphTypeface gt = null;
            //    item.TryGetGlyphTypeface(out gt);
            //   // MessageBox.Show(item.ToString());
            //    FontSource fs = new FontSource(gt);

            //}

            this.cmbEditorStatus.SelectionChanged += new SelectionChangedEventHandler(cmbEditorStatus_SelectionChanged);
            this.ckbToolBarVisibility.Unchecked += new RoutedEventHandler(ckbToolBarVisibility_Unchecked);
            this.ckbToolBarVisibility.Checked += new RoutedEventHandler(ckbToolBarVisibility_Checked);
            this.btnGetXaml.Click += new RoutedEventHandler(btnGetXaml_Click);
            this.btnOpenXaml.Click += new RoutedEventHandler(btnOpenXaml_Click);

        }

        void btnOpenXaml_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Rich xaml Files(Xml) | *.xrich";
            openFileDialog.Multiselect = false;

            if (openFileDialog.ShowDialog() == true)
            {
                if (openFileDialog.File != null)
                {
                    FileStream fileStream = openFileDialog.File.OpenRead();
                    byte[] bytes = new byte[fileStream.Length];
                    fileStream.Read(bytes, 0, bytes.Length);
                    fileStream.Close();

                    string xaml = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    this.editor.SetXaml(xaml);
                }
            }
        }

        void btnGetXaml_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.editor.GetXaml());
        }

        void ckbToolBarVisibility_Checked(object sender, RoutedEventArgs e)
        {
            this.ckbToolBarVisibilityChange();
        }

        void ckbToolBarVisibility_Unchecked(object sender, RoutedEventArgs e)
        {
            this.ckbToolBarVisibilityChange();
        }

        void ckbToolBarVisibilityChange()
        {
            if (this.ckbToolBarVisibility.IsChecked.HasValue && this.ckbToolBarVisibility.IsChecked.Value)
            {
                this.editor.ToolBarVisibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.editor.ToolBarVisibility = System.Windows.Visibility.Collapsed;
            }
        }

        void cmbEditorStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((System.Windows.Controls.ContentControl)(this.cmbEditorStatus.SelectedItem)).Content.ToString() == "Edit")
            {
                this.editor.EditorStatus = Beetle.Sl.EditorControl.Core.EditorStatusEnum.Edit;
            }
            else if (((System.Windows.Controls.ContentControl)(this.cmbEditorStatus.SelectedItem)).Content.ToString() == "Designer")
            {
                this.editor.EditorStatus = Beetle.Sl.EditorControl.Core.EditorStatusEnum.Designer;
            }
            else
            {
                this.editor.EditorStatus = Beetle.Sl.EditorControl.Core.EditorStatusEnum.View;
            }
        }
    }
}
